<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link
      rel="stylesheet"
      href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
    />
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>

    <link
      href="https://cdn.bootcss.com/xterm/3.11.0/xterm.css"
      rel="stylesheet"
    />
    <script src="https://cdn.bootcss.com/xterm/3.11.0/xterm.js"></script>

    <style>
      .mt_20 {
        margin-top: 20px;
      }

      .one .panel-body {
        padding: 0;
      }

      #content {
        height: 400px;
        /* overflow: scroll; */
      }

      /* .xterm-screen,
      .xterm-text-layer,
      .xterm-selection-layer,
      .xterm-link-layer,
      .xterm-cursor-layer {
        width: 100% !important;
      } */
    </style>
  </head>
  <body>
    <div class="container mt_20">
      <div class="panel panel-default one">
        <div class="panel-heading">消息内容框</div>
        <div class="panel-body">
          <div id="content"></div>
        </div>
      </div>

      <div class="panel panel-default two">
        <div class="panel-heading">发送框</div>
        <div class="panel-body">
          <input
            id="sendMsg"
            class="form-control inline"
            style="display: inline;"
            type="text"
          />
          <button id="btn_send" class="btn btn-success mt_20">发送</button>
        </div>
      </div>
    </div>

    <script src="https://cdn.bootcss.com/socket.io/2.1.0/socket.io.js"></script>
    <script type="text/javascript">
      var term = new Terminal();
      term.open(document.getElementById("content"));
      // term.write("hello,webcome!");

      //47.98.187.80
      //连接服务端，因为本机使用localhost 所以connect(url)中url可以不填或写 http://localhost
      var socket = io.connect("http://47.103.36.143:8888");

      // 监听 receiveMsg 事件，用来接收其他客户端推送的消息
      socket.on("server_client_msg", function(data) {
        console.log(data, "=========server_client_msg==========");
        term.write(data.msg);
      });

      var content = document.getElementById("content");
      var sendMsg = document.getElementById("sendMsg");
      var btn_send = document.getElementById("btn_send");

      function sendMsgToServer() {
        var data = {
          client: "王孟贵",
          msg: sendMsg.value
        };

        //给服务端发送 sendMsg事件名的消息
        socket.emit("client_server_msg", data);

        sendMsg.value = "";
      }

      btn_send.addEventListener("click", sendMsgToServer);

      $("#sendMsg").on("keydown", function(e) {
        console.log(e.keyCode, "=========e.keyCode=========");
        if (e.keyCode == 13) {
          sendMsgToServer();
        }
      });
    </script>
  </body>
</html>
